package com.william.jdbcplus.boot.test.controller;

import com.william.jdbcplus.boot.starter.util.DaoUtil;
import com.william.jdbcplus.boot.starter.util.DbUtil;
import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootWrap;
import com.william.jdbcplus.boot.test.dto.R;
import com.william.jdbcplus.boot.test.entity.Result;
import com.william.jdbcplus.common.page.Page;
import com.william.jdbcplus.core.toolkit.UuidUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;
import java.util.List;

/**
 * @author niliwei
 */
@Api(value = "结果测试")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("/result")
public class ResultController {
    @GetMapping(value = "/addByList")
    R<String> addByList() {
        int count = DaoUtil.BASE.countAll(Result.class);
        // 1、开始时间
        long beginTime = System.currentTimeMillis();
        int limit = 10000;
        for (int i = 1; i <= count / limit + 1; i++) {
            Page<Result> page = new Page<>(i, limit);
            QueryLambdaBootWrap bootstrap = new QueryLambdaBootWrap();
            bootstrap
                    .selectAll(Result.class)
                    .from(Result.class)
                    .orderByAsc(Result::getId);

            Page<Result> list = bootstrap.queryPage(page, Result.class);
            DaoUtil.BASE.addList(list.getRecords());
        }

        // 1、结束时间
        long endTime = System.currentTimeMillis();
        // 3、消耗的时间
        long consumeTime = endTime - beginTime;
        String msg = String.format(" { 总数量：%d 耗时 %d 毫秒}--------------------", count, consumeTime);
        log.info(msg);

        return R.data(msg);
    }

    @GetMapping(value = "/addByOne")
    R<String> addByOne() {
        List<Result> list = DaoUtil.BASE.listAll(Result.class);
        DaoUtil.BASE.deleteAll(Result.class);

        int count = list.size();

        // 1、开始时间
        long beginTime = System.currentTimeMillis();

        for (Result result : list) {
            DaoUtil.BASE.add(result);
        }

        // 1、结束时间
        long endTime = System.currentTimeMillis();
        // 3、消耗的时间
        long consumeTime = endTime - beginTime;
        String msg = String.format(" { 总数量：%d 耗时 %d 毫秒}--------------------", count, consumeTime);
        log.info(msg);
        return R.data(msg);
    }

    @GetMapping(value = "/insertBatch")
    R<String> insertBatch() throws Exception {
        List<Result> list = DaoUtil.BASE.listAll(Result.class);

        int count = list.size();


        //DataBaseManager类是我自己封装的一个工具类，用于获取、关闭数据库资源，
        //大家如果只是自己测试用没必要，用DriverManager操作即可
        DataSource ds = DbUtil.datasource;
        Connection connection = ds.getConnection();

        //这里必须设置为false，我们手动批量提交
        connection.setAutoCommit(false);

        //这里需要注意，SQL语句的格式必须是预处理的这种，就是values(?,?,...,?)，否则批处理不起作用
        PreparedStatement stm = connection.prepareStatement("insert into lis_result\n" +
                "\t(result_id, test_id, thirdplat_id, test_num, test_date\n" +
                "\t, test_barcode, inst_id, inst_code, inst_name, item_id\n" +
                "\t, item_name, item_name_cn, item_name_en, item_code, item_name_out\n" +
                "\t, item_code_out, item_namecn_out, item_nameen_out, item_channel, result_symbol\n" +
                "\t, item_result, item_result_text, item_remark, item_od, item_sco\n" +
                "\t, item_result4, item_unit, item_ref_min, item_ref_max, item_ref\n" +
                "\t, item_limit_min, item_limit_max, item_method, item_flag, item_flag_str\n" +
                "\t, print_grp, print_sort, domain_id, hosp_id, hosp_code\n" +
                "\t, hosp_name, dsthosp_id, dsthosp_code, dsthosp_name, modlfy_flag\n" +
                "\t, check_flag, crt_flag, neg_text, his_flag, his_result\n" +
                "\t, his_result_time, tip_ratio, input_type, result_time, input_pc\n" +
                "\t, input_userid, input_user_name, print_flag, order_id, order_item_id\n" +
                "\t, order_item_code, order_item_name)\n" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?\n" +
                "\t, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        System.out.println("开始插入数据");
        // 1、开始时间
        long beginTime = System.currentTimeMillis();
        for (Result result : list) {
            if (result.getId() != null) {
                stm.setString(1, UuidUtil.get32UUID());
            } else {
                stm.setNull(1, Types.VARCHAR);
            }
            if (result.getTestId() != null) {
                stm.setString(2, result.getTestId());
            } else {
                stm.setNull(2, Types.VARCHAR);
            }
            if (result.getThirdPlatId() != null) {
                stm.setLong(3, result.getThirdPlatId());
            } else {
                stm.setNull(3, Types.BIGINT);
            }
            if (result.getTestNum() != null) {
                stm.setString(4, result.getTestNum());
            } else {
                stm.setNull(4, Types.VARCHAR);
            }
            if (result.getTestDate() != null) {
                stm.setDate(5, new java.sql.Date(result.getTestDate().getTime()));
            } else {
                stm.setNull(5, Types.DATE);
            }
            if (result.getBarcode() != null) {
                stm.setString(6, result.getBarcode());
            } else {
                stm.setNull(6, Types.VARCHAR);
            }
            if (result.getInstId() != null) {
                stm.setLong(7, result.getInstId());
            } else {
                stm.setNull(7, Types.BIGINT);
            }
            if (result.getInstCode() != null) {
                stm.setString(8, result.getInstCode());
            } else {
                stm.setNull(8, Types.VARCHAR);
            }
            if (result.getInstName() != null) {
                stm.setString(9, result.getInstName());
            } else {
                stm.setNull(9, Types.VARCHAR);
            }
            if (result.getItemId() != null) {
                stm.setLong(10, result.getItemId());
            } else {
                stm.setNull(10, Types.BIGINT);
            }
            if (result.getItemName() != null) {
                stm.setString(11, result.getItemName());
            } else {
                stm.setNull(11, Types.VARCHAR);
            }
            if (result.getItemNameCn() != null) {
                stm.setString(12, result.getItemNameCn());
            } else {
                stm.setNull(12, Types.VARCHAR);
            }
            if (result.getItemNameEn() != null) {
                stm.setString(13, result.getItemNameEn());
            } else {
                stm.setNull(13, Types.VARCHAR);
            }
            if (result.getItemCode() != null) {
                stm.setString(14, result.getItemCode());
            } else {
                stm.setNull(14, Types.VARCHAR);
            }
            if (result.getItemNameOut() != null) {
                stm.setString(15, result.getItemNameOut());
            } else {
                stm.setNull(15, Types.VARCHAR);
            }
            if (result.getItemCodeOut() != null) {
                stm.setString(16, result.getItemCodeOut());
            } else {
                stm.setNull(16, Types.VARCHAR);
            }
            if (result.getItemNameCnOut() != null) {
                stm.setString(17, result.getItemNameCnOut());
            } else {
                stm.setNull(17, Types.VARCHAR);
            }
            if (result.getItemNameEnOut() != null) {
                stm.setString(18, result.getItemNameEnOut());
            } else {
                stm.setNull(18, Types.VARCHAR);
            }
            if (result.getItemChannel() != null) {
                stm.setString(19, result.getItemChannel());
            } else {
                stm.setNull(19, Types.VARCHAR);
            }
            if (result.getResultSymbol() != null) {
                stm.setString(20, result.getResultSymbol());
            } else {
                stm.setNull(20, Types.VARCHAR);
            }
            if (result.getItemResult() != null) {
                stm.setDouble(21, result.getItemResult());
            } else {
                stm.setNull(21, Types.DOUBLE);
            }
            if (result.getItemResultText() != null) {
                stm.setString(22, result.getItemResultText());
            } else {
                stm.setNull(22, Types.VARCHAR);
            }
            if (result.getItemRemark() != null) {
                stm.setString(23, result.getItemRemark());
            } else {
                stm.setNull(23, Types.VARCHAR);
            }
            if (result.getItemOd() != null) {
                stm.setString(24, result.getItemOd());
            } else {
                stm.setNull(24, Types.VARCHAR);
            }
            if (result.getItemSco() != null) {
                stm.setString(25, result.getItemSco());
            } else {
                stm.setNull(25, Types.VARCHAR);
            }
            if (result.getItemResult4() != null) {
                stm.setString(26, result.getItemResult4());
            } else {
                stm.setNull(26, Types.VARCHAR);
            }
            if (result.getItemUnit() != null) {
                stm.setString(27, result.getItemUnit());
            } else {
                stm.setNull(27, Types.VARCHAR);
            }
            if (result.getItemRefMin() != null) {
                stm.setDouble(28, result.getItemRefMin());
            } else {
                stm.setNull(28, Types.DOUBLE);
            }
            if (result.getItemRefMax() != null) {
                stm.setDouble(29, result.getItemRefMax());
            } else {
                stm.setNull(29, Types.DOUBLE);
            }
            if (result.getItemRef() != null) {
                stm.setString(30, result.getItemRef());
            } else {
                stm.setNull(30, Types.VARCHAR);
            }
            if (result.getItemLimitMin() != null) {
                stm.setDouble(31, result.getItemLimitMin());
            } else {
                stm.setNull(31, Types.DOUBLE);
            }
            if (result.getItemLimitMax() != null) {
                stm.setDouble(32, result.getItemLimitMax());
            } else {
                stm.setNull(32, Types.DOUBLE);
            }
            if (result.getItemMethod() != null) {
                stm.setString(33, result.getItemMethod());
            } else {
                stm.setNull(33, Types.VARCHAR);
            }
            if (result.getItemFlag() != null) {
                stm.setInt(34, result.getItemFlag());
            } else {
                stm.setNull(34, Types.INTEGER);
            }
            if (result.getItemFlagStr() != null) {
                stm.setString(35, result.getItemFlagStr());
            } else {
                stm.setNull(35, Types.VARCHAR);
            }
            if (result.getPrintGroup() != null) {
                stm.setLong(36, result.getPrintGroup());
            } else {
                stm.setNull(36, Types.BIGINT);
            }
            if (result.getPrintSort() != null) {
                stm.setLong(37, result.getPrintSort());
            } else {
                stm.setNull(37, Types.BIGINT);
            }
            if (result.getDomainId() != null) {
                stm.setLong(38, result.getDomainId());
            } else {
                stm.setNull(38, Types.BIGINT);
            }
            if (result.getHospId() != null) {
                stm.setLong(39, result.getHospId());
            } else {
                stm.setNull(39, Types.BIGINT);
            }
            if (result.getHospCode() != null) {
                stm.setString(40, result.getHospCode());
            } else {
                stm.setNull(40, Types.VARCHAR);
            }
            if (result.getHospName() != null) {
                stm.setString(41, result.getHospName());
            } else {
                stm.setNull(41, Types.VARCHAR);
            }
            if (result.getDstHospId() != null) {
                stm.setLong(42, result.getDstHospId());
            } else {
                stm.setNull(42, Types.BIGINT);
            }
            if (result.getDstHospCode() != null) {
                stm.setString(43, result.getDstHospCode());
            } else {
                stm.setNull(43, Types.VARCHAR);
            }
            if (result.getDstHospName() != null) {
                stm.setString(44, result.getDstHospName());
            } else {
                stm.setNull(44, Types.VARCHAR);
            }
            if (result.getModifyFlag() != null) {
                stm.setInt(45, result.getModifyFlag());
            } else {
                stm.setNull(45, Types.INTEGER);
            }
            if (result.getCheckFlag() != null) {
                stm.setInt(46, result.getCheckFlag());
            } else {
                stm.setNull(46, Types.INTEGER);
            }
            if (result.getCrtFlag() != null) {
                stm.setInt(47, result.getCrtFlag());
            } else {
                stm.setNull(47, Types.INTEGER);
            }
            if (result.getNegText() != null) {
                stm.setString(48, result.getNegText());
            } else {
                stm.setNull(48, Types.VARCHAR);
            }
            if (result.getHisFlag() != null) {
                stm.setInt(49, result.getHisFlag());
            } else {
                stm.setNull(49, Types.INTEGER);
            }
            if (result.getTestId() != null) {
                stm.setString(50, result.getHisResult());
            } else {
                stm.setNull(50, Types.VARCHAR);
            }
            if (result.getHisResultTime() != null) {
                stm.setDate(51, new java.sql.Date(result.getHisResultTime().getTime()));
            } else {
                stm.setNull(51, Types.DATE);
            }
            if (result.getTipRatio() != null) {
                stm.setInt(52, result.getTipRatio());
            } else {
                stm.setNull(52, Types.INTEGER);
            }
            if (result.getInputType() != null) {
                stm.setInt(53, result.getInputType());
            } else {
                stm.setNull(53, Types.INTEGER);
            }
            if (result.getResultTime() != null) {
                stm.setDate(54, new java.sql.Date(result.getResultTime().getTime()));
            } else {
                stm.setNull(54, Types.DATE);
            }
            if (result.getInputPc() != null) {
                stm.setString(55, result.getInputPc());
            } else {
                stm.setNull(55, Types.VARCHAR);
            }
            if (result.getInputUserId() != null) {
                stm.setLong(56, result.getInputUserId());
            } else {
                stm.setNull(56, Types.BIGINT);
            }
            if (result.getInputUserName() != null) {
                stm.setString(57, result.getInputUserName());
            } else {
                stm.setNull(57, Types.VARCHAR);
            }
            if (result.getPrintFlag() != null) {
                stm.setInt(58, result.getPrintFlag());
            } else {
                stm.setNull(58, Types.INTEGER);
            }
            if (result.getOrderId() != null) {
                stm.setString(59, result.getOrderId());
            } else {
                stm.setNull(59, Types.VARCHAR);
            }
            if (result.getOrderItemId() != null) {
                stm.setLong(60, result.getOrderItemId());
            } else {
                stm.setNull(60, Types.BIGINT);
            }
            if (result.getOrderItemCode() != null) {
                stm.setString(61, result.getOrderItemCode());
            } else {
                stm.setNull(61, Types.VARCHAR);
            }
            if (result.getOrderItemName() != null) {
                stm.setString(62, result.getOrderItemName());
            } else {
                stm.setNull(62, Types.VARCHAR);
            }

            //将要执行的SQL语句先添加进去，不执行
            stm.addBatch();
        }
        //100W条SQL语句已经添加完成，执行这100W条命令并提交
        stm.executeBatch();
        connection.commit();
        connection.close();
        stm.close();
        // 1、结束时间
        long endTime = System.currentTimeMillis();
        // 3、消耗的时间
        long consumeTime = endTime - beginTime;
        String msg = String.format(" { 总数量：%d 耗时 %d 毫秒}--------------------", count, consumeTime);
        log.info(msg);
        return R.data(msg);
    }
}
